package com.furentech.iotserver.fa.server;

import com.furentech.iotserver.fa.server.handler.ChannelInit;
import com.furentech.iotserver.fa.server.handler.SinopecFaEncoder;
import com.furentech.iotserver.fa.server.handler.SinopecFaDecoder;
import com.furentech.iotserver.fa.server.handler.SinopecHandler;


import com.furentech.iotserver.fa.sinopec.SinopecParam;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleStateHandler;
import io.netty.util.concurrent.EventExecutorGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class UserChannelInitializer extends ChannelInitializer<SocketChannel> {

    @Value("${netty.read-timeout}")
    private int readTimeOut;

    @Autowired
    @Qualifier("businessGroup")//开了50个线程来确保并发性
    private EventExecutorGroup businessGroup;


    @Autowired
    SinopecParam sinopecParam;

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline pipeline = socketChannel.pipeline();
        pipeline.addLast(new IdleStateHandler(readTimeOut, 0, 0, TimeUnit.MINUTES));

        pipeline.addLast(new ChannelInit());

        // FA E0 E0
        pipeline.addLast(
                new SinopecFaDecoder(sinopecParam.destiAddr)
        );

        pipeline.addLast(new SinopecHandler());
        pipeline.addLast(new SinopecFaEncoder());
    }
}
